#include <51func.h>
void *threadFunc(void *arg){
    pthread_mutex_t *pmutex = (pthread_mutex_t *)arg;
    sleep(1);
    pthread_mutex_lock(pmutex);
    printf("Child\n");
    pthread_mutex_unlock(pmutex);
}
int main(int argc, char *argv[])
{
    pthread_mutexattr_t mutexattr; // 1 为锁属性申请内存
    pthread_mutexattr_init(&mutexattr); // 2 初始化锁属性
    pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_RECURSIVE); // 3 锁属性设置为可重入锁
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex,&mutexattr); // 4 初始化出来的是可重入锁
    
    pthread_t tid;
    pthread_create(&tid,NULL,threadFunc,&mutex);

    int ret = pthread_mutex_lock(&mutex);
    THREAD_ERROR_CHECK(ret,"lock1");
    printf("Parent Lock1\n");
    ret = pthread_mutex_lock(&mutex);
    THREAD_ERROR_CHECK(ret,"lock2");
    printf("Parent Lock2\n");
    
    pthread_mutex_unlock(&mutex);
    sleep(3);
    printf("Parent unLock2\n");
    pthread_mutex_unlock(&mutex);
    
    pthread_join(tid,NULL);
    return 0;
}

